package com.norma.macaron.service.user.service;

import com.google.common.collect.Maps;
import com.norma.macaron.common.model.enums.CommonCode;
import com.norma.macaron.common.model.response.ResponseT;
import com.norma.macaron.service.common.client.user.UserServiceClient;
import com.norma.macaron.service.common.domain.auth.UserExt;
import com.norma.macaron.service.common.domain.user.TMenu;
import com.norma.macaron.service.common.domain.user.TRole;
import com.norma.macaron.service.common.domain.user.TUser;
import com.norma.macaron.service.common.domain.user.TUserRole;
import com.norma.macaron.service.common.repository.user.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author zhangchaofeng
 * @date 2020/6/3 0003
 * @description
 */
@RestController
@Service
public class UserServiceImpl implements UserServiceClient {

    @Autowired
    UserRepository userRepository;
    @Autowired
    RoleRepository roleRepository;
    @Autowired
    PermissionRepository permissionRepository;
    @Autowired
    UserRoleRepository userRoleRepository;
    @Autowired
    MenuRepository menuRepository;

    @Override
    @PostMapping("/v1.0/user/sayHello")
    public Map<String, Object> sayHello(@RequestParam("userName") String userName) {
        System.out.println("hello world~");
        HashMap<String, Object> resMap = Maps.newHashMap();
        resMap.put("msg", "你好," + userName);
        return resMap;
    }

    @Override
    public UserExt getUserext(String username) {
        return null;
    }

    @Override
    @GetMapping("/v1.0/user/getUserById")
    public ResponseT<TUser> getUserById(@RequestParam("userId") Long userId) {
        TUser user = userRepository.getOne(userId);
        return new ResponseT(CommonCode.SUCCESS, user);
    }


    /**
     * @param phone
     * @return
     * @description 获取用户信息以及角色, 权限信息
     */
    @Override
    @PostMapping("/v1.0/user/getUserExtByPhone")
    public UserExt getUserExtByPhone(@RequestParam("phone") String phone) {
        //user info
        TUser user = userRepository.getByPhone(phone);
        if (user == null) {
            return new UserExt();
        }
        //role info
        List<TUserRole> userRole = userRoleRepository.getByUserId(String.valueOf(user.getId()));
        if (CollectionUtils.isEmpty(userRole)) {
            //return empty object
            return new UserExt(user, null, null);
        }
        List<String> roleIds = userRole.stream().map(x -> x.getRoleId()).distinct().collect(Collectors.toList());
        //roles info
        List<TRole> roles = roleRepository.queryRolesInIds(roleIds.toArray(new String[roleIds.size()]));

        if (CollectionUtils.isEmpty(roles)) {
            return new UserExt(user, null, null);
        }
        //permission info
        Set<String> permissions = permissionRepository.getMenuIdInRoleId(roleIds.toArray(new String[roleIds.size()]));
        if (CollectionUtils.isEmpty(permissions)) {
            return new UserExt(user, roles, null);
        }
        //menu info
        List<TMenu> tMenus = menuRepository.queryByIds(permissions.toArray(new String[permissions.size()]));
        if (CollectionUtils.isEmpty(tMenus)) {
            return new UserExt(user, roles, null);
        }
        return new UserExt(user, roles, tMenus);
    }
}
